package com.pe.adexus.portal3g.servlets;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.pe.adexus.portal3g.componente.LeerXml;

public class FlashServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public FlashServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.proceso(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.proceso(request, response);
	}

	public void proceso(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		try{
		
			String mimeType = "application/ x-shockwave- flash";
			response.setContentType(mimeType) ;
			
			//"C:\\imagenes\\Adexus.jpg"
			File file = (File)session.getAttribute("filePromocion");
			response.setContentLength((int)file. length()) ;
		        
			
			//System.out.println("file="+file);
			FileInputStream in = new FileInputStream(file);
			OutputStream out = response.getOutputStream();
		       
	        byte[] buf = new byte[1024];
	        int count = 0;
	        while ((count = in.read(buf) ) >= 0) {
	            out.write(buf, 0, count);
	        }
	        in.close();
	        out.close();
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
	
	}

	
	
	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occure
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
